<template>
	<div class="auto-trade">
		<bread-crumb divider>
			<template slot="title">卡券核销</template>
		</bread-crumb>
		<div class="form-wrap" v-loading="loading">
			<el-form
				:model="submitData"
				ref="myForm"
				size="small"
				:inline="false" label-width="120px" labelPosition="left" class="demo-form-inline"
			>
				<el-form-item label="卡券码:" prop="merchantName">
					<el-input v-model.trim="submitData.couponCode" placeholder="请输入卡券码" class="w300"></el-input>
					<el-button type="success" @click="getCouponDetail()">加载</el-button>
				</el-form-item>

				<el-form-item label="卡券id:" >{{submitData.couponId}}</el-form-item>
				<el-form-item label="卡券状态:" >
					<span class="c-red" v-if="submitData.useStatus==1">已核销</span>
					<span class="c-green" v-else>可用</span>
				</el-form-item>

				<el-form-item label="商户名称:" >{{submitData.merchantName}}</el-form-item>

				<el-form-item label="卡券名称:" >{{submitData.stockName}}</el-form-item>

				<el-form-item label="卡券价值:" >{{submitData.discountAmount}}<span>元</span></el-form-item>

				<el-form-item label="使用门槛:" >{{submitData.transactionMinimum}}<span>元</span></el-form-item>

			<!--	用户信息-->
				<el-form-item label="用户头像:" ><img :src="submitData.avatar" height="60"></el-form-item>
				<el-form-item label="用户名称:" >{{submitData.name}}</el-form-item>
				<el-form-item label="openId:" >{{submitData.openId}}</el-form-item>
				<el-form-item label="所属员工:" >{{submitData.userid}}</el-form-item>

			</el-form>
			<el-button @click="submitForm" type="primary">提交</el-button>
		</div>
		<!--<div>{{submitData}}</div>-->
	</div>
</template>

<script>
import {
	apiCouponCreateOfBusiness, apiGetCouponDetailForUse, apiUseCouponBusiness,
} from '@/axios/api.system';
import { mixins } from '@/mixins/mixins.global';
import { mixinUtils } from '@/mixins/mixins.utils';
import breadCrumb from '@/views/components/breadCrumb';
import { isEmpty } from "@/libs/util";

export default {
	name: 'couponUse',
	mixins:[mixins,mixinUtils],
	components:{breadCrumb},
	data() {

		return {
			submitData: {
				//主要信息
				couponCode:"",//
				couponId:"",//
				openId:"",//

			},
		}
	},
	created() {
	},
	methods:{


		//查询卡券信息
		getCouponDetail(){
			let _couponCode = this.submitData.couponCode
			if(isEmpty(_couponCode)==false){
				apiGetCouponDetailForUse({couponCode:_couponCode}).then((res)=>{
					if(res.resultFlag==true){
						this.submitData = {...res.data}
						this.notify('成功',res.resultMsg,'success')
					}else{
						this.notify('错误',res.resultMsg,'error')
					}
				})
			}else{
				this.$message.error("没有输入卡券")
			}
		},

		//表单提交
		submitForm() {
			let _couponCode = this.submitData.couponCode
			let _couponId   = this.submitData.couponId
			let _openId     = this.submitData.openId
			//限制
			if (isEmpty(_couponId) || isEmpty(_openId)) {
				this.$message.error("请先加载信息")
				return false
			}

			if(this.submitData.useStatus==1){
				this.$message.error("此卡券已经使用过")
				return false
			}

			//提交
			this.confirm('确定要核销这张券吗？',()=> {
				apiUseCouponBusiness({
					openId   : _openId,
					couponId : _couponId,
					couponCode: _couponCode,
				}).then((res)=>{
					if(res.resultFlag==true){
						this.notify('成功',res.resultMsg,'success')
					}else{
						this.notify('错误',res.resultMsg,'error')
					}
				})
			})

		},
		resetForm(formName) {
			this.$refs[formName].resetFields();
		}
	}
};
</script>

<style lang="less" scoped>
.el-form-item--mini.el-form-item, .el-form-item--small.el-form-item{
	margin-bottom: 0px!important;
}
</style>
